<template>
  <div class="table">
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item><i class="el-icon-tickets"></i> 问题管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">

        <el-input v-model="select_word" placeholder="根据问题模糊搜索" class="handle-input mr10"></el-input>
        <el-button type="primary" icon="search" @click="search">搜索</el-button>
      </div>
      <el-table :data="data" border style="width: 100%" ref="tableData" @selection-change="handleSelectionChange">
        <!--<el-table-column type="selection" width="55"></el-table-column>-->
        <el-input type="hidden" prop="id" sortable/>

        <el-table-column prop="contentStr"  label="问题" sortable >
        </el-table-column>
        <el-table-column prop="strTime" label="提问时间" width="120">
        </el-table-column>
        <el-table-column prop="doctorName" label="医生名称"width="120" >
        </el-table-column>
        <el-table-column prop="deptName" label="部门名称" width="120">
        </el-table-column>
        <el-table-column label="操作" width="280">
          <template slot-scope="scope">
            <el-button size="small" @click="handleQuest(scope.$index, scope.row)">查看问题详情</el-button>
            <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">屏蔽问题</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination @current-change="handleCurrentChange" layout="prev, pager, next" :total="total" >
        </el-pagination>
      </div>
    </div>

    <!-- 查看详情弹出框 -->
    <el-dialog title="查看详情" :visible.sync="editVisible" width="80%">
      <el-form ref="form" :model="form" label-width="100px">

        <el-form-item label="问题内容">
          <el-input type="textarea"  readonly v-model="form.content"></el-input>
        </el-form-item>

        <el-form-item label="提问时间">
          <el-input v-model="form.strTime" readonly></el-input>
        </el-form-item>

`
      
      </el-form>

    </el-dialog>



    <!-- 屏蔽提示框 -->
    <el-dialog title="提示" :visible.sync="delVisible" width="300px" center>
      <div class="del-dialog-cnt">屏蔽后无法恢复，是否确定屏蔽？</div>
      <span slot="footer" class="dialog-footer">
                <el-button @click="delVisible = false">取 消</el-button>
                <el-button type="primary" @click="deleteRow">确 定</el-button>
            </span>
    </el-dialog>

  </div>
</template>

<script>

  export default {
    data() {
      return {
        url: './static/vuetable.json',
        tableData: [],
        cur_page: 1,
        total:1,
        multipleSelection: [],
        select_cate: '',
        select_word: '',
        del_list: [],
        is_search: false,
        editVisible: false,
        delVisible: false,
        form: {
          strTime:'',
          content: '',
          id:'',

        },
        commentList:[],
        idx: -1

      }
    },
    created() {
      this.getData();
    },
    computed: {
      data() {
        return this.tableData.filter((d) => {
          let is_del = false;
          for (let i = 0; i < this.del_list.length; i++) {
            if (d.name === this.del_list[i].name) {
              is_del = true;
              break;
            }
          }
          if (!is_del) {
            return d;
          }
        })
      }
    },
    methods: {
      // 分页导航
      handleCurrentChange(val) {
        this.cur_page = val;
        this.getData();
      },
      add(){
        this.$router.push({name:'/dept/add'})
      },
      queryByName(){
        let hospital = JSON.parse(sessionStorage.getItem('userMap')).hospital;
        var that = this;
        this.HttpUtil.encryptGet(
          this,
          this.Global.URLPATH.QUEST_QUERY,
          {
            hospital :hospital,
            token:sessionStorage.getItem("token"),
            pageNum:that.cur_page,
            content:this.select_word
          },
          function (data) {
            data = JSON.parse(data.data);
            that.total = data.total;
            that.handleData(data);
          },
          function (data) {//访问失败
            that.tableData = [];
            that.total = 1;
            that.$message.error(data.msg);
          });
      },
      // 获取 easy-mock 的模拟数据
      getData() {
        let hospital = JSON.parse(sessionStorage.getItem('userMap')).hospital;
        var that = this;
        this.HttpUtil.encryptGet(
          this,
          this.Global.URLPATH.QUEST_QUERY,
          {
            hospital :hospital,
            token:sessionStorage.getItem("token"),
            pageNum:that.cur_page
          },
          function (data) {
            data =data.data;
            that.total = data.total;
            that.handleData(data);
          },
          function (data) {//访问失败
            that.tableData = [];
            that.total = 1;
            //that.$message.success(data.msg);
          });
      },
      handleData(data){
        for(var i in data.list){
          if(data.list[i].content.length >30){
            data.list[i].contentStr=data.list[i].content.substring(0,30)+"...";
          }else{
            data.list[i].contentStr = data.list[i].content;
          }
          data.list[i].deptName = JSON.parse(data.list[i].json).deptName;
          data.list[i].doctorName = JSON.parse(data.list[i].json).doctorName;
        }
        this.tableData = data.list;
      },
      search() {
        this.is_search = true;
        this.cur_page = 1;
        this.queryByName();

      },

      formatter(row, column) {
        return row.address;
      },
      filterTag(value, row) {
        return row.tag === value;
      },
      handleQuest(index, row) {
        const item = this.tableData[index];
        sessionStorage.setItem("quest_id",item.id);
        this.$router.push({path:'/quest/detail'})
      },

      handleDelete(index, row) {
        this.idx = index;
        this.delVisible = true;
        const item = this.tableData[index];
        this.form.id = item.id;
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      // 确定删除
      deleteRow(){
        var that = this;
        this.HttpUtil.encryptPost(
          this,
          this.Global.URLPATH.QUEST_UPDATE,
          {
            hospital :JSON.parse(sessionStorage.getItem('userMap')).hospital,
            token:sessionStorage.getItem("token"),
            id:this.form.id,
            status:0
          },
          function (data) {
            that.$message.success("禁用成功");
            that.getData();
            that.delVisible=false;
          },
          function (data) {//访问失败

            that.$message.error(data.msg);
          });

      }
    },
    watch:{
      $route(newValue, oldValue){
        if(newValue.path == "/quest"){
          this.getData();
        }
      }
    }
  }

</script>

<style scoped>
  .handle-box {
    margin-bottom: 20px;
  }

  .handle-select {
    width: 120px;
  }

  .handle-input {
    width: 300px;
    display: inline-block;
  }
  .del-dialog-cnt{
    font-size: 16px;
    text-align: center
  }
</style>
